{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ef8702bc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Error processing https://github.com/graphcore/poptorch: 404 Client Error: Not Found for url: https://oss.open-digger.cn/github/graphcore/poptorch/openrank.json\n",
      "Error processing https://github.com/naver/roma: 404 Client Error: Not Found for url: https://oss.open-digger.cn/github/naver/roma/openrank.json\n",
      "Error processing https://github.com/TorchDrift/TorchDrift: 404 Client Error: Not Found for url: https://oss.open-digger.cn/github/TorchDrift/TorchDrift/openrank.json\n",
      "Processed file saved to items_with_2025_value_2.csv\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd  \n",
    "import requests  \n",
    "import json  \n",
    "  \n",
    "# 读取CSV文件  \n",
    "file_path = 'items.csv'  \n",
    "df = pd.read_csv(file_path)  \n",
    "  \n",
    "# 定义一个函数来处理github_repo列并获取2024-09至2025-08的总和\n",
    "def get_2025_value(repo_url):  \n",
    "    if pd.isna(repo_url):  \n",
    "        return None  \n",
    "    \n",
    "    # 提取仓库路径  \n",
    "    repo_path = repo_url.split('github.com/')[-1].rstrip('/')  \n",
    "    \n",
    "    # 构造下载URL  \n",
    "    download_url = f'https://oss.open-digger.cn/github/{repo_path}/openrank.json'  \n",
    "    \n",
    "    try:  \n",
    "        # 下载JSON数据  \n",
    "        response = requests.get(download_url)  \n",
    "        response.raise_for_status()  \n",
    "        json_data = response.json()  \n",
    "        \n",
    "        # 计算2024-09至2025-08的总和\n",
    "        total = 0\n",
    "        count = 0\n",
    "        \n",
    "        # 检查数据是否是字典格式\n",
    "        if isinstance(json_data, dict):\n",
    "            for key, value in json_data.items():\n",
    "                # 检查键是否符合YYYY-MM格式\n",
    "                if len(key) == 7 and key.startswith(('2024-', '2025-')):\n",
    "                    year_month = key.split('-')\n",
    "                    year = int(year_month[0])\n",
    "                    month = int(year_month[1])\n",
    "                    \n",
    "                    # 检查是否是2024-09到2025-08期间\n",
    "                    if (year == 2024 and month >= 9) or (year == 2025 and month <= 8):\n",
    "                        try:\n",
    "                            total += float(value)\n",
    "                            count += 1\n",
    "                        except (ValueError, TypeError):\n",
    "                            continue\n",
    "        \n",
    "        # 如果没有找到任何数据，返回None\n",
    "        return total if count > 0 else None\n",
    "        \n",
    "    except Exception as e:  \n",
    "        print(f'Error processing {repo_url}: {e}')  \n",
    "        return None   \n",
    "  \n",
    "# 应用函数并创建新列  \n",
    "df['2025_value'] = df['github_repo'].apply(get_2025_value)  \n",
    "  \n",
    "# 保存修改后的CSV文件  \n",
    "output_file_path = 'items_with_2025_value_2.csv'  \n",
    "df.to_csv(output_file_path, index=False)  \n",
    "  \n",
    "print(f'Processed file saved to {output_file_path}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "676861f9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "github_repo列中非空值的数量: 119\n"
     ]
    }
   ],
   "source": [
    "import csv\n",
    "\n",
    "def count_non_empty_github_repos(input_file):\n",
    "    count = 0\n",
    "    \n",
    "    with open(input_file, mode='r', newline='', encoding='utf-8') as csvfile:\n",
    "        reader = csv.DictReader(csvfile)\n",
    "        \n",
    "        # 检查是否存在github_repo列\n",
    "        if 'github_repo' not in reader.fieldnames:\n",
    "            print(\"错误：CSV文件中没有'github_repo'列\")\n",
    "            return\n",
    "        \n",
    "        # 读取所有行并计数\n",
    "        for row in reader:\n",
    "            if row['github_repo'].strip():  # 检查是否为空或仅包含空白字符\n",
    "                count += 1\n",
    "    \n",
    "    print(f\"github_repo列中非空值的数量: {count}\")\n",
    "\n",
    "# 使用示例\n",
    "input_csv = 'items.csv'\n",
    "count_non_empty_github_repos(input_csv)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
